//
//  GlobalNavigationController.m
//  Salvation
//
//  Created by 高波 on 2018/9/12.
//  Copyright © 2018年 Ouuki. All rights reserved.
//

#import "GlobalNavigationController.h"
#import "UIImage+ResizingImage.h"
@interface GlobalNavigationController ()

@end

@implementation GlobalNavigationController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UINavigationBar *bar = [UINavigationBar appearance];
    [bar setBackgroundImage:[UIImage resizingImageWithName:@"naviBg"] forBarMetrics:UIBarMetricsDefault];
    
    [self.navigationBar setBarTintColor:[UIColor blackColor]];
    UIColor * color = [UIColor blackColor];
    UIFont *font = [UIFont boldSystemFontOfSize:18.0f];
    NSDictionary * dict=[[NSDictionary alloc] initWithObjectsAndKeys:
                         color,NSForegroundColorAttributeName,
                         font,NSFontAttributeName, nil];
    self.navigationBar.titleTextAttributes = dict;
    [self.navigationBar setTintColor:color];
    self.navigationBar.translucent = NO;
    
    
    //导航栏背景透明
    //    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    
    //导航栏底部线清楚
    //    self.navigationBar.barStyle = UIBarStyleBlack;
    //    self.navigationBar.translucent = YES;
    //    [self.navigationBar setShadowImage:[UIImage new]];
    
    
    self.interactivePopGestureRecognizer.delegate = self;
    self.interactivePopGestureRecognizer.enabled = YES;
}

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
    GlobalNavigationController *nvc = [super initWithRootViewController:rootViewController];
    self.interactivePopGestureRecognizer.delegate = self;
    nvc.delegate = self;
    return nvc;
}

-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (navigationController.viewControllers.count == 1)
        self.currentShowVC = Nil;
    else
        self.currentShowVC = viewController;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        return (self.currentShowVC == self.topViewController); //the most important
    }
    return YES;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:animated];
    if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) {
        viewController.navigationItem.leftBarButtonItem = [self createBackButton];
    }
}

- (UIBarButtonItem *)createBackButton
{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 20, 20);
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [backButton addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
    return back;
}

- (void)popself
{
    [self popViewControllerAnimated:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

@end
